package com.fxu.framework.core.enums;

import lombok.NoArgsConstructor;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 枚举验证实现
 * @author fangxu
 * @version 1.0.0
 */
@NoArgsConstructor
public class EnumValidator implements ConstraintValidator<EnumValid, Object> {
    private EnumValid annotation;

    @Override
    public void initialize(EnumValid constraintAnnotation) {
        this.annotation = constraintAnnotation;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
        // return this.annotation.canNull();
        // return Arrays.stream(this.annotation.target().getEnumConstants()).anyMatch(v -> v.code().equals(value));
        return null == value || null == this.annotation.target() ||
                BaseEnum.findOne(this.annotation.target().getEnumConstants(), v -> v.code().equals(value)) != null;
    }
}